<?php
class WebResponse extends CApplicationComponent {
    
    const
        USER_REGISTER_SUCCESS = 10,
        USER_REGISTER_FAIL = 11,
        USER_REGISTER_RESET_ACTIVATION = 12,
        USER_ACTIVATE_EXPIRED = 20,
        USER_ACTIVATE_SUCCESS = 21,
        USER_ACTIVATE_ACTIVATED = 22,
        USER_ACTIVATE_FAIL = 23,
        PASSWORD_RECOVERY_SUCCESS = 30,
        PASSWORD_RECOVERY_FAIL = 31,
        PASSWORD_RECOVERY_URL_SUCCESS=32,
        PASSWORD_RECOVERY_URL_EXPIRED=33,
        ENQUIRY_SUCCESS = 40,
        ENQUIRY_FAIL = 41,
        BUSINESS_REGISTER_SUCCESS = 50,
        BUSINESS_REGISTER_FAIL = 51,
        CONTEST_SUBMIT_SUCCESS=60,
        STATE_NAME = 'response'
    ;
    public $responseAction;
    public $paramName='responseId_';
    
    public function type($code, $params=array()){
        
        if(!empty($params))
            Yii::app()->user->setState(self::STATE_NAME, CJSON::encode($params));
        
        Yii::app()->controller->redirect(Yii::app()->createUrl($this->responseAction, array(
            $this->paramName=>$code,
        )));
    }
    
    public function getCode(){
        return Yii::app()->request->getParam($this->paramName);
    }
    
    public function getParams(){
        return Yii::app()->user->hasState(self::STATE_NAME)? 
            CJSON::decode(Yii::app()->user->getState(self::STATE_NAME)):array();
    }
      
    public function getPage(){
        switch ($this->getCode()) {
            // user registration
            case self::USER_REGISTER_SUCCESS:
                return 'userRegisterSuccess';
                break;
            case self::USER_REGISTER_FAIL: // user registration fail
                return 'userRegisterFail';
                break;
            case self::USER_REGISTER_RESET_ACTIVATION:
                return 'userRegisterResetActivation';
                break;
            
            // user activation
            case self::USER_ACTIVATE_EXPIRED:
                return 'userActivationExpired';
                break;
            case self::USER_ACTIVATE_SUCCESS:
                return 'userActivationSuccess';
                break;
            case self::USER_ACTIVATE_ACTIVATED:
                return 'userActivationActivated';
                break;
            case self::USER_ACTIVATE_FAIL:
                return 'userActivationFail';
                break;
            
            // password recovery
            case self::PASSWORD_RECOVERY_SUCCESS:
                return 'userRecoverySuccess';
                break;
            case self::PASSWORD_RECOVERY_FAIL:
                return 'userRecoveryFail';
                break;
            case self::PASSWORD_RECOVERY_URL_SUCCESS:
                return 'userSetNewPasswordSuccess';
                break;
            case self::PASSWORD_RECOVERY_URL_EXPIRED:
                return 'userSetNewPasswordExpired';
                break;
            
            // enquiry
            case self::ENQUIRY_SUCCESS:
                return 'EnquirySubmitSuccess';
                break;
            case self::ENQUIRY_FAIL:
                return 'EnquirySubmitFail';
                break;

            default:
                HttpStatus::code(404);
                break;
        }//end switch
    }
}//end class